package com.geek.javaCource.demo.shardingsphere;

import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Create by chenhd on 2021/9/21.
 */
@Component
public class ShardingService {
    @Resource
    DataSource dataSource;

    public boolean insert(String sql) throws SQLException {
        Statement statement = dataSource.getConnection().createStatement();
        return statement.execute(sql);
    }

    public List<Map<String, Object>> query() throws SQLException {
        List<Map<String, Object>> resultList = new ArrayList<>();
        String sql = "select * from `order`";
        Statement statement = dataSource.getConnection().createStatement();
        ResultSet resultSet = statement.executeQuery(sql);
        while (resultSet.next()) {
            Map<String, Object> line = new HashMap<>();
            line.put("id", resultSet.getLong("id"));
            line.put("totalPrice", resultSet.getDouble("total_price"));
            resultList.add(line);
        }
        return resultList;
    }
}
